package com.tom.demo.array;

public class Test704_Search {

    public static void main(String[] args) {
        int[] nums = {-1, 0, 3, 5, 9, 12};
        System.out.println(search(nums, 9));
        System.out.println(search(nums, 2));
        System.out.println(search(nums, 5));
    }

    public static int search(int[] nums, int target) {
        int start = 0, mid;
        int end = nums.length - 1;

        int idx = -1;
        while ((mid = (start + end) / 2) >= 0 && start <= end) {
            if (nums[mid] == target) {
                idx = mid;
                break;
            } else if (nums[mid] > target) {
                end = mid - 1;
            } else {
                start = mid + 1;
            }
        }
        return idx;
    }


}
